//
//  BN_WeekFood.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-15.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_WeekFood.h"
#import "NurserySchoolCellTableViewCell.h"

@implementation BN_WeekFood {
    NSArray *titleArr_;
    NSArray *contentArr_;
    NSArray *timeArr_;
    NSMutableArray *netArr_;
}

- (id)init {
    if (self = [super init]) {
        if (!_kinderGartenHelper) {
            _kinderGartenHelper = [[BN_KinderGartenHelper alloc]init];
        }
        if ([self respondsToSelector:@selector(requestNetData)]) {
            [self requestNetData];
        }
        netArr_ = [[NSMutableArray alloc]init];
    }
    return self;
}

#pragma mark UITableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NurserySchoolCellTableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return netArr_.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"NurserySchoolCellTableViewCell";
    BOOL nibsRegistered = NO;
    if (!nibsRegistered) {
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([NurserySchoolCellTableViewCell class]) bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
        nibsRegistered = YES;
    }
    NurserySchoolCellTableViewCell *cell = (NurserySchoolCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.tag = indexPath.section;

    // 重用BUG
    cell.contImageView.image = [UIImage imageNamed:@""];
    cell.titleLabel.text = @"";

    // 顶部分割线视图
    UILabel *titleLabel = (UILabel *)[cell.dividerView viewWithTag:402];
    if (![netArr_[indexPath.section][@"foodDate"] isEqual:[NSNull null]]) {
        NSString *dateStr = netArr_[indexPath.section][@"foodDate"];
        NSRange rang = NSMakeRange(0, 19);
        NSString *strRang = [dateStr substringWithRange:rang];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        NSTimeZone *timeZone = [NSTimeZone localTimeZone];
        [formatter setTimeZone:timeZone];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *dateTime = [formatter dateFromString:strRang];
        titleLabel.text = [IMB_TimeFormatModel timeFormat:dateTime];
        [cell initDividerView];
    }
    
    // 图片
    NSString *fileTUrlStr = nil;
    NSArray *files = netArr_[indexPath.section][@"files"];
    if (files.count > 0) {
        fileTUrlStr = files[0][@"fileTUrl"];
        fileTUrlStr = [THUMBNAIL_BASEURL stringByAppendingString:fileTUrlStr];
    }
    NSURL *fileTUrl = [NSURL URLWithString:fileTUrlStr];
    [cell.contImageView setImageWithURL:fileTUrl placeholderImage:nil];
    
    // 美食内容文字
    if (![netArr_[indexPath.section][@"foodCont"] isEqual:[NSNull null]]) {
        NSString *contStr = netArr_[indexPath.section][@"foodCont"];
        if (contStr.length > 0) {
            cell.contLabel.text = contStr;
        }else {
            cell.contLabel.text = @"暂无介绍";
        }
    }else {
        cell.contLabel.text = @"美食";
    }
    
    // foodId
    cell.foodId = [NSString stringWithFormat:@"%@",netArr_[indexPath.section][@"foodId"]];
    
    [cell toGroup];
    [cell.contLabel sizeToFit];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark
#pragma mark 网络数据
- (void)requestNetData {
    __weak typeof(self) weakSelf = self;
    _kinderGartenHelper.queryFoodListCompleteHandler = ^(id result,NSString* errorMessage){
        //update by jason yan 2014-05-30
        if (errorMessage) {
            [LZCHINT title:errorMessage type:LZCError duration:2 places:HINT_PLACES callBack:NULL];
            weakSelf.reloadList(NO,NO,nil);
        }
        if(result){
            [weakSelf loadData:result];
        }
    };
    [_kinderGartenHelper refreshFoodList];

}

- (void)loadData:(id)info {
    netArr_ = (NSMutableArray *)info;
    self.reloadList(YES,NO,nil);
}

#pragma mark 
#pragma mark 通知
- (void)foodModelRemove:(NSString *)result {
    NSInteger tag = [result intValue];
    [netArr_ removeObjectAtIndex:tag];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:tag];
    NSArray *indexArray = [NSArray arrayWithObject:indexPath];
    self.reloadList(NO,YES,indexArray);
}

#pragma mark
#pragma mark 刷新类
- (void)upLoadFinished:(MJRefreshBaseView *)refreshView {
    // 列表开始加载，开始数据请求
    // [self loadNextNetData];
    
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [refreshView endRefreshing];
}

- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    // 列表开始刷新，开始数据请求
    [self requestNetData];
    
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [refreshView endRefreshing];
}

- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    switch ([refreshView viewType]) {
        case -1:
            [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:0.85];
            break;
        case 1:
            [self performSelector:@selector(upLoadFinished:) withObject:refreshView afterDelay:0];
            break;
        default:
            break;
    }
}

- (void)refreshViewEndRefreshing:(MJRefreshBaseView *)refreshView
{
}

- (void)refreshView:(MJRefreshBaseView *)refreshView stateChange:(MJRefreshState)state
{
    switch (state) {
        case MJRefreshStateNormal:

            break;
            
        case MJRefreshStatePulling:
                        break;
            
        case MJRefreshStateRefreshing:
            
            break;
        default:
            break;
    }
}

#pragma mark
#pragma mark 刷新
- (void)toSchoolReloadList:(School_ReloadList)reloadList {
    self.reloadList = reloadList;
}

- (void)foodRefresh {
    if (!_kinderGartenHelper) {
        _kinderGartenHelper = [[BN_KinderGartenHelper alloc]init];
    }
    [self requestNetData];
}

- (void)dealloc
{
    NSLog(@"每周美食Modal释放...");
}


@end
